package leetcode;

/**
 * https://leetcode-cn.com/problems/island-perimeter/description/
 * https://leetcode.com/problems/island-perimeter/?tab=Description
 * @program: brushing
 * @description:
 * @author: LiuQiang
 * @create: 2018-07-25 10:36
 **/
public class IslandPerimeter {
    public int test(int[][] grid) {
        int yMax = grid.length;
        int xMax = grid[0].length;

        int count = 0;
        for (int i = 0; i < yMax; i++) {
            for (int j = 0; j < xMax; j++) {
                // System.out.println("坐标:"+"x:"+j+"y:"+i+" "+grid[i][j]);
                int flag = grid[i][j];
                if (flag != 0) {
                    //X 左
                    if (j > 0) {
                        if (grid[i][j - 1] == 0) {
                            count++;
                        }
                    } else {
                        count++;
                    }
                    //X 右
                    if (j + 1 < xMax) {
                        if (grid[i][j + 1] == 0) {
                            count++;
                        }
                    } else {
                        count++;
                    }

                    //Y 上
                    if (i > 0) {
                        if (grid[i - 1][j] == 0) {
                            count++;
                        }
                    } else {
                        count++;
                    }

                    //Y 下
                    if (i + 1 < yMax) {
                        if (grid[i + 1][j] == 0) {
                            count++;
                        }
                    } else {
                        count++;
                    }
                }
            }
        }

        return count;
    }

    public static void main(String[] args) {
        int[][] a =
                {
                        {0, 1, 0, 0},
                        {1, 1, 1, 0},
                        {0, 1, 0, 0},
                        {1, 1, 0, 0}};

        IslandPerimeter I = new IslandPerimeter();
        System.out.println(I.test(a));
    }
}
